import tkinter as tk
from tkinter import messagebox


class LoginRegisterSystem:
    def __init__(self, root):
        self.root = root
        self.root.title("Login/Register System")

        # 登录界面元素
        self.login_frame = tk.Frame(self.root, padx=10, pady=10)
        self.login_frame.pack(padx=20, pady=20)

        tk.Label(self.login_frame, text="Login", font=("Arial", 18)).grid(row=0, column=1, columnspan=2)
        tk.Label(self.login_frame, text="Username:").grid(row=1, column=0)
        self.login_username_entry = tk.Entry(self.login_frame)
        self.login_username_entry.grid(row=1, column=1, columnspan=2)

        tk.Label(self.login_frame, text="Password:").grid(row=2, column=0)
        self.login_password_entry = tk.Entry(self.login_frame, show="*")
        self.login_password_entry.grid(row=2, column=1, columnspan=2)

        login_button = tk.Button(self.login_frame, text="Login", command=self.login)
        login_button.grid(row=3, column=1, sticky="ew", padx=5, pady=5)

        # 注册界面元素
        self.register_frame = tk.Frame(self.root, padx=10, pady=10)

        tk.Label(self.register_frame, text="Register", font=("Arial", 18)).grid(row=0, column=1, columnspan=2)
        tk.Label(self.register_frame, text="Username:").grid(row=1, column=0)
        self.register_username_entry = tk.Entry(self.register_frame)
        self.register_username_entry.grid(row=1, column=1, columnspan=2)

        tk.Label(self.register_frame, text="Password:").grid(row=2, column=0)
        self.register_password_entry = tk.Entry(self.register_frame, show="*")
        self.register_password_entry.grid(row=2, column=1, columnspan=2)

        register_button = tk.Button(self.register_frame, text="Register", command=self.register)
        register_button.grid(row=3, column=1, sticky="ew", padx=5, pady=5)

        self.register_frame.pack_forget()  # 默认隐藏注册界面

        # 登录注册按钮
        login_register_button = tk.Button(self.root, text="Register", command=self.show_register)
        login_register_button.pack(pady=10)

    def show_register(self):
        self.login_frame.pack_forget()
        self.register_frame.pack(padx=20, pady=20)

    def login(self):
        username = self.login_username_entry.get()
        password = self.login_password_entry.get()

        # 在这里可以添加验证逻辑，比如检查用户名密码是否正确

        messagebox.showinfo("Login", f"Welcome, {username}!")

    def register(self):
        username = self.register_username_entry.get()
        password = self.register_password_entry.get()

        # 在这里可以添加注册逻辑，比如将用户名密码存储到数据库

        messagebox.showinfo("Register", "Registration successful!")
        self.register_frame.pack_forget()
        self.show_login()

    def show_login(self):
        self.register_frame.pack_forget()
        self.login_frame.pack(padx=20, pady=20)


if __name__ == "__main__":
    root = tk.Tk()
    app = LoginRegisterSystem(root)
    root.mainloop()
